package scenetracer.core.light;

import java.util.ArrayList;
import java.util.List;

import scenetracer.core.Ray;
import scenetracer.core.primitive.IntersectionState;
import scenetracer.math.Point2;
import scenetracer.math.Point3;
import scenetracer.math.Vector3;

public class PointLight extends Light {

	private Point3 location;
	public final int FACTOR = 7;
	
	public PointLight(Point3 location){
		this.location = location;	
	}
	
	public Point3 getLocation(){
		return this.location;		
	}
	
	public void setLocation(Point3 location){
		this.location = location;
	}
	
	
	@Override
	public boolean Intersect(Ray ray, IntersectionState intersection) {	
		
		Vector3 rayLightDir = Point3.sub(this.location, ray.getOrigin());
		rayLightDir.normalize();
		
		if(!ray.getDirection().equals(rayLightDir)){
			return false;	
		}
		
		intersection.setIntersectionPoint(this.location);		
		return true;
	}


	@Override
	public Vector3 getNormal(IntersectionState i) {
		return null;
	}

	@Override
	public Point2 getTextureCoords(Point3 point) {
		return null;
	}


	@Override
	public List<Vector3> getDirLights(IntersectionState intersection) {
		ArrayList<Vector3> dirs = new ArrayList<Vector3>();
		Vector3 lightDir = Point3.sub(location, intersection.getIntersectionPoint(), new Vector3());
		lightDir.normalize();
		dirs.add(lightDir);
		return dirs;
	}

	@Override
	public float getAtte(Point3 point) {
		
		
		float lightDistance = point.distanceTo(location);
		float factorLight = (1 / this.getPower())*FACTOR;
		float atte = 1 / (lightDistance*(factorLight + factorLight*lightDistance));
		return atte > 1 ? 1 : atte;
	}
	public PointLight moveLocation(float x, float y, float z) {
		PointLight l = new PointLight(new Point3(location.x + x, location.y + y, location.z + z));
		l.setColor(super.getColor()); 
		
		return l;
	}
	
	public String toString(){
		
		return "Light Location:" + location.toString();
	}
}
